package leetcode.editor.cn;
//给你两棵二叉树： root1 和 root2 。 
//
// 想象一下，当你将其中一棵覆盖到另一棵之上时，两棵树上的一些节点将会重叠（而另一些不会）。你需要将这两棵树合并成一棵新二叉树。合并的规则是：如果两个节点重叠
//，那么将这两个节点的值相加作为合并后节点的新值；否则，不为 null 的节点将直接作为新二叉树的节点。 
//
// 返回合并后的二叉树。 
//
// 注意: 合并过程必须从两个树的根节点开始。 
//
// 
//
// 示例 1： 
// 
// 
//输入：root1 = [1,3,2,5], root2 = [2,1,3,null,4,null,7]
//输出：[3,4,5,5,4,null,7]
// 
//
// 示例 2： 
//
// 
//输入：root1 = [1], root2 = [1,2]
//输出：[2,2]
// 
//
// 
//
// 提示： 
//
// 
// 两棵树中的节点数目在范围 [0, 2000] 内 
// -10⁴ <= Node.val <= 10⁴ 
// 
//
// Related Topics 树 深度优先搜索 广度优先搜索 二叉树 👍 1361 👎 0


//leetcode submit region begin(Prohibit modification and deletion)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 * int val;
 * TreeNode left;
 * TreeNode right;
 * TreeNode() {}
 * TreeNode(int val) { this.val = val; }
 * TreeNode(int val, TreeNode left, TreeNode right) {
 * this.val = val;
 * this.left = left;
 * this.right = right;
 * }
 * }
 */
class Solution246 {

    public TreeNode mergeTrees(TreeNode node1, TreeNode node2) {
        if (node1 == null || node2 == null) {
            return node1 != null ? node1 : node2;
        }

        node1.val = node1.val + node2.val;
        node1.left = dfs(node1.left, node2.left);
        node1.right = dfs(node1.right, node2.right);
        return node1;
    }

    public TreeNode mergeTrees3(TreeNode root1, TreeNode root2) {
        return dfs(root1, root2);
    }

    private TreeNode dfs(TreeNode node1, TreeNode node2) {
        if (node1 == null || node2 == null) {
            return node1 != null ? node1 : node2;
        }

        node1.val = node1.val + node2.val;
        node1.left = dfs(node1.left, node2.left);
        node1.right = dfs(node1.right, node2.right);
        return node1;


    }

    public TreeNode mergeTrees2(TreeNode root1, TreeNode root2) {
        return dfs2(root1, root2);
    }

    private TreeNode dfs2(TreeNode node1, TreeNode node2) {
        if (node1 == null || node2 == null) {
            return node1 != null ? node1 : node2;
        }
        TreeNode root = new TreeNode();
        root.val = node1.val + node2.val;
        root.left = dfs2(node1.left, node2.left);
        root.right = dfs2(node1.right, node2.right);
        return root;
    }

    public TreeNode mergeTrees1(TreeNode root1, TreeNode root2) {
        return dfs1(root1, root2);
    }

    private TreeNode dfs1(TreeNode node1, TreeNode node2) {
        if (node1 == null && node2 == null) {
            return null;
        }
        TreeNode root = new TreeNode();
        if (node1 != null && node2 != null) {
            root.val = node1.val + node2.val;
            root.left = dfs1(node1.left, node2.left);
            root.right = dfs1(node1.right, node2.right);
        } else if (node1 != null) {
            root.val = node1.val;
            root.left = dfs1(node1.left, null);
            root.right = dfs1(node1.right, null);
        } else {
            root.val = node2.val;
            root.left = dfs1(null, node2.left);
            root.right = dfs1(null, node2.right);
        }
        return root;
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
//leetcode submit region end(Prohibit modification and deletion)
